package xq.gwt.ui.widgets.factory;

import java.util.ArrayList;


import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.form.fields.FormItem;

import xq.gwt.mvc.model.BooleanPropertyModel;
import xq.gwt.mvc.model.CommandModel;
import xq.gwt.mvc.model.DatePropertyModel;
import xq.gwt.mvc.model.EntityArrayPropertyModel;
import xq.gwt.mvc.model.LongTextPropertyModel;
import xq.gwt.mvc.model.PasswordPropertyModel;
import xq.gwt.mvc.model.PropertyModel;
import xq.gwt.mvc.model.StringArrayPropertyModel;
import xq.gwt.mvc.model.TimePropertyModel;
import xq.gwt.ui.widgets.command.ButtonCommandPresenter;
import xq.gwt.ui.widgets.command.ButtonItemCommandPresenter;
import xq.gwt.ui.widgets.property.CheckBoxItemPropertyPresenter;
import xq.gwt.ui.widgets.property.ComboBoxItemPropertyPresenter;
import xq.gwt.ui.widgets.property.DateItemPropertyPresenter;
import xq.gwt.ui.widgets.property.EntityListItemPropertyPresenter;
import xq.gwt.ui.widgets.property.FormItemPropertyPresenter;
import xq.gwt.ui.widgets.property.MultiSelectItemPropertyPresenter;
import xq.gwt.ui.widgets.property.PasswordItemPropertyPresenter;
import xq.gwt.ui.widgets.property.TextAreaItemPropertyPresenter;
import xq.gwt.ui.widgets.property.TextItemPropertyPresenter;
import xq.gwt.ui.widgets.property.TimeItemPropertyPresenter;

public class FormItemPropertyPresenterFactory {

	public static FormItemPropertyPresenter buildPresenter(PropertyModel property){
		if (property == null)
			return null;
		if (property.getValueItems() != null && property instanceof StringArrayPropertyModel){
			MultiSelectItemPropertyPresenter presenter = new MultiSelectItemPropertyPresenter();
			presenter.setModel((StringArrayPropertyModel)property);
			return presenter;
		}else if (property.getValueItems() != null){
			ComboBoxItemPropertyPresenter presenter = new ComboBoxItemPropertyPresenter();
			presenter.setModel(property);
			return presenter;
		}		
		else if(property instanceof PasswordPropertyModel){
			PasswordItemPropertyPresenter presenter = new PasswordItemPropertyPresenter();
			presenter.setModel((PasswordPropertyModel)property);
			return presenter;
		}
		else if(property instanceof BooleanPropertyModel){
			CheckBoxItemPropertyPresenter presenter = new CheckBoxItemPropertyPresenter();
			presenter.setModel((BooleanPropertyModel)property);
			return presenter;
		}		
		else if(property instanceof TimePropertyModel){
			TimeItemPropertyPresenter presenter = new TimeItemPropertyPresenter();
			presenter.setModel((TimePropertyModel)property);			
			return presenter;
		}		
		else if(property instanceof DatePropertyModel){
			DateItemPropertyPresenter presenter = new DateItemPropertyPresenter();
			presenter.setModel((DatePropertyModel)property);
			return presenter;
		}
		else if(property instanceof StringArrayPropertyModel){
			MultiSelectItemPropertyPresenter presenter = new MultiSelectItemPropertyPresenter();
			presenter.setModel((StringArrayPropertyModel)property);
			return presenter;
		}
		else if(property instanceof LongTextPropertyModel){
			TextAreaItemPropertyPresenter presenter = new TextAreaItemPropertyPresenter();
			presenter.setModel(property);			
			return presenter;
		}
		else if(property instanceof EntityArrayPropertyModel){
			EntityListItemPropertyPresenter presenter = new EntityListItemPropertyPresenter();
			presenter.setModel((EntityArrayPropertyModel)property);			
			return presenter;
		}
		else{
			TextItemPropertyPresenter presenter = new TextItemPropertyPresenter();
			presenter.setModel(property);			
			return presenter;
			
		}			
	}
	
	public static FormItemPropertyPresenter[] buildPresenters(PropertyModel[] props){
		if(props == null)
			return null;
		ArrayList<FormItemPropertyPresenter> formItemList = new ArrayList<FormItemPropertyPresenter>();
		for (int i = 0; i < props.length; i++) {
			if(!props[i].isHidden()){
				formItemList.add(buildPresenter(props[i]));
			}
		}
				
		FormItemPropertyPresenter[] formItems = new FormItemPropertyPresenter[formItemList.size()];
		formItemList.toArray(formItems);
		return formItems;		
	}
	
	public static FormItem[] buildFormItems(PropertyModel[] props){
		if(props == null)
			return null;
		ArrayList<FormItemPropertyPresenter> formItemList = new ArrayList<FormItemPropertyPresenter>();
		for (int i = 0; i < props.length; i++) {
			if(!props[i].isHidden()){
				formItemList.add(buildPresenter(props[i]));
			}
		}
		FormItem[] formItems = new FormItem[formItemList.size()];
		for (int i = 0; i < formItemList.size(); i++) {
			formItems[i] = formItemList.get(i).getFormItem();
		}		
		return formItems;		
	}
	
	public static FormItem[] buildFormItems(CommandModel[] commands){
		if(commands == null){
			return null;
		}
		
		FormItem[] formItems = new FormItem[commands.length];
		for (int i = 0; i < commands.length; i++) {			
			ButtonItemCommandPresenter presenter = new ButtonItemCommandPresenter();
			presenter.setModel(commands[i]);
			formItems[i] = presenter.getButton();
		}
		
		return formItems;		
	}
	
	public static IButton[] buildButtons(CommandModel[] commands){
		if(commands == null){
			return null;
		}
		
		IButton[] buttons = new IButton[commands.length];
		for (int i = 0; i < commands.length; i++) {			
			ButtonCommandPresenter presenter = new ButtonCommandPresenter();
			presenter.setModel(commands[i]);
			buttons[i] = presenter.getButton();
		}
		
		return buttons;		
	}
}
